package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.ApplicationsDao;
import org.example.Pojo.Applications;
import org.example.Service.ApplicationsService;
import org.example.Utils.UnionResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class ApplicationsImpl implements ApplicationsService {
    @Resource
    ApplicationsDao applicationsDao;
    @Override
    public UnionResult CreateApplication(Applications application) {
        application.setFlags(0);
        application.setTimestamps(System.currentTimeMillis());
        if (applicationsDao.insert(application)>0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult QueryMyApplication(Applications application) {
        QueryWrapper<Applications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("binduser",application.getBinduser());
        queryWrapper.orderByAsc("timestamps");
        List<Applications> list = applicationsDao.selectList(queryWrapper);
        return UnionResult.Success("",list);
    }

    @Override
    public UnionResult QueryUserApplication(Applications application) {
        QueryWrapper<Applications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flags",0);
        List<Applications> list = applicationsDao.selectList(queryWrapper);
        return UnionResult.Success("",list);
    }

    @Override
    public UnionResult CancelApplication(Applications application) {
        if (applicationsDao.updateById(application)>0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult GetApplication(Applications application) {
        QueryWrapper<Applications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",application.getUid());
        Applications applications = applicationsDao.selectOne(queryWrapper);
        return UnionResult.Success("",applications);
    }

    @Override
    public UnionResult UpdateApplication(Applications application) {
        if (applicationsDao.updateById(application)>0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult UploadApplicationFile(MultipartFile file) throws IOException {
        File dir = new File("src/main/resources/imgs");
        String fileid = UUID.randomUUID().toString().replace("-", "");
        String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();
        File targetFile = new File(dir.getCanonicalPath(), fileid+fileExtension);
        try {
            file.transferTo(targetFile);
            return UnionResult.Success("",fileid+fileExtension);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public UnionResult GetPassed() {
        QueryWrapper<Applications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flags",1);
        return UnionResult.Success("",applicationsDao.selectList(queryWrapper));
    }
}
